#!/bin/bash

# 镜像列表
images=(
   "nginx"
   "ubuntu"
   # "alpine"
   # "busybox"
   # "mysql"
   # "redis"
   # "httpd"
   # "python"
   # "node"
   # "golang"
   # "openjdk"
   # "centos"
   # "fedora"
   # "debian"
   # "postgres"
   # "mongo"
   # "rabbitmq"
   # "elasticsearch"
   # "kibana"
   # "nginx:alpine"
   # "wordpress"
   # "php"
   # "memcached"
   # "jenkins"
   # "grafana"
   # "prometheus"
   # "influxdb"
   # "consul"
   # "vault"
   # "cassandra"
   # "cockroachdb"
   # "apache"
   # "spark"
   # "hadoop"
   # "flume"
   # "kafka"
   # "zookeeper"
   # "sqlite"
   # "rancher"
   # "traefik"
   # "openresty"
   # "tgi"
   # "busybox:glibc"
   # "mcr.microsoft.com/dotnet/aspnet"
   # "mcr.microsoft.com/dotnet/sdk"
   # "mcr.microsoft.com/windows/servercore"
   # "mcr.microsoft.com/windows/nanoserver"
   # "mcr.microsoft.com/mssql/server"
   # "venom"
   # "cypress/browsers"
   # "opencv"
   # "minio/minio"
   # "harness/drone"
   # "bitnami/mariadb"
   # "openvpn"
   # "graylog/graylog"
   # "sonarqube"
   # "onbasis/pythondock"
   # "openjdk:8"
   # "openjdk:11"
   # "typescript"
   # "golang:alpine"
)

# 日志文件
log_file="docker_image_download.log"
# 结果报告
report_file="docker_image_report.txt"

# 清空之前的日志文件和报告文件
echo "#!/bin/bash" > "$log_file"
echo "" >> "$log_file"
echo "下载镜像报告" > "$report_file"
echo "======================" >> "$report_file"
echo "镜像名         状态          大小          推送状态" >> "$report_file"
echo "------------------------------------------" >> "$report_file"

# 总镜像数量
total_images=${#images[@]}

# 循环下载并推送
for i in "${!images[@]}"; do
    image="${images[$i]}"
    remaining=$((total_images - i - 1))

    echo "下载镜像: $image (剩余: $remaining)" | tee -a "$log_file"
    if docker pull "$image"; then
        size=$(docker inspect "$image" --format='{{.Size}}' | awk '{print $1/1024/1024 "MB"}')
        echo "镜像下载成功: $image" | tee -a "$log_file"
        docker tag "$image" "localhost:5000/$image"
        if docker push "localhost:5000/$image"; then
            echo "镜像推送成功: localhost:5000/$image" | tee -a "$log_file"
            echo "$image         成功          $size          成功" >> "$report_file"
        else
            echo "镜像推送失败: localhost:5000/$image" | tee -a "$log_file"
            echo "$image         推送失败      $size          失败" >> "$report_file"
        fi
    else
        echo "镜像下载失败: $image" | tee -a "$log_file"
        echo "错误: 在下载镜像时出现问题: $image" | tee -a "$log_file"
        echo "$image         下载失败      N/A            N/A" >> "$report_file"
    fi
done

echo "下载完成！报告已保存到 $report_file"

